
/*
function callBack(xhr){
	//等待上传结果,将背景图像设置为tx2.jpg 
	if (xhr.readyState == 1) {
		//filenode.parentNode.style.backgroundImage = "url('/images/tx2.jpg')";  
	}  
	//上传成功，返回的文件名，设置到父节点的背景中  
	if (xhr.readyState == 4 && xhr.status == 200) { 
		//var path=JSON.parse(xhr.responseText).path.split("\\");
		//filenode.parentNode.style.backgroundImage = "url('./uploads/" +path[path.length-1]+ "')";  
	}
}
*/
var ftpUrl = "http://172.16.1.52:3000/";
var getFtpUrl = "http://172.16.1.52:3001/";/**读取上传成功后的图片IP地址*/
/**
elementName:元素名称
path:需要的路径 如 img, file，word等
async:async false同步 true异步
callBack 回调函数
*/
function sendFileToFtp(elementName, path, async, callBack){
	var files = document.getElementsByName(elementName);
	var length = files.length;
	var functionName = "";
	if(length == 0){
		console.error("sendFileToFtp:请检查file控件的 name值是否正确.")
		return;
	}else{
		if(length==1){
			functionName = "upload";
		}else{
			functionName = "uploadMany";
		}
	}
	var xhr = new XMLHttpRequest();
	
    //设置回调，当请求的状态发生变化时，就会被调用  
	xhr.onreadystatechange = function () {
        callBack(xhr);
    }
	var have = 0;
    //构造form数据 
    var data = new FormData(); 
	data.append("ftpFileToDiskPath",path);
	for(var i=0; i<files.length;i++){
		var filenode = files[i];
		var filevalue = filenode.files[0];
		if(filevalue != undefined && filevalue != ""){
			data.append("file", filevalue);
			have = have +1;
		}
	}
	if(have >= 1){
		//设置请求，true：表示异步  
		xhr.open("post", ftpUrl+functionName, async);
		//提交请求  
		xhr.send(data);
	}else{
		console.log("sendFileToFtp: 当前无有效的文件.不进行提交")
	}  
}

/*
多文件结果（最多12个文件）：
{"flg":1,"files":[
	{"fieldname":"file","originalname":"6.jpg","encoding":"7bit","mimetype":"image/jpeg","destination":"../ftpwordir","filename":"file-1510732468799.jpg","path":"ftpwordir\\file-1510732468799.jpg","size":8370},
	{"fieldname":"file","originalname":"8.jpg","encoding":"7bit","mimetype":"image/jpeg","destination":"../ftpwordir","filename":"file-1510732468800.jpg","path":"ftpwordir\\file-1510732468800.jpg","size":10742}
	]
}
--失败：
{flg:0,errMsg:"错误信息"}

单文件结果
{"flg":1,"file":{"fieldname":"file","originalname":"8.jpg","encoding":"7bit","mimetype":"image/jpeg","destination":"../ftpwordir","filename":"file-1510737180766.jpg","path"ftpwordir\\file-1510737180766.jpg","size":10742}}
失败：
{flg:0,errMsg:"错误信息"}

*/